﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYFamilyLibDataManager;

namespace ModelValidation.FireCompartment
{
	public partial class DlgFireOutlineWinDoorWallFamilyReplace : System.Windows.Forms.Form
	{
		public DlgFireOutlineWinDoorWallFamilyReplace(ExternalCommandData cmdData)
		{
			this.m_doc = cmdData.Application.ActiveUIDocument.Document;
			this.m_cmdData = cmdData;
			this.InitializeComponent();
		}

		private void treeViewDoor_AfterCheck(object sender, TreeViewEventArgs e)
		{
			this.treeSingleCheck(this.treeViewDoor, sender, e);
		}

		private void treeViewWindow_AfterCheck(object sender, TreeViewEventArgs e)
		{
			this.treeSingleCheck(this.treeViewWindow, sender, e);
		}

		private void treeViewRollingDoor_AfterCheck(object sender, TreeViewEventArgs e)
		{
			this.treeSingleCheck(this.treeViewRollingDoor, sender, e);
		}

		private void treeSingleCheck(TreeView tree, object sender, TreeViewEventArgs e)
		{
			if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard)
			{
				foreach (object obj in tree.Nodes)
				{
					TreeNode treeNode = (TreeNode)obj;
					if (treeNode != e.Node)
					{
						treeNode.Checked = false;
					}
				}
			}
		}

		private void DlgFireOutlineWinDoorWallFamilyReplace_Load(object sender, EventArgs e)
		{
			this.radioFamilyProject.Checked = true;
			this.UpdateTreeShow();
		}

		private void radioFamilyLib_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateTreeShow();
		}

		private void radioFamilyProject_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateTreeShow();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void UpdateTreeShow()
		{
			try
			{
				this.treeViewDoor.Nodes.Clear();
				this.treeViewRollingDoor.Nodes.Clear();
				this.treeViewWindow.Nodes.Clear();
				if (this.radioFamilyLib.Checked)
				{
					List<string> list = (from e in this.m_lstFamilyLibDoors
					select e.Name).ToList<string>();
					List<string> list2 = (from e in this.m_lstFamilyLibDoorsRolling
					select e.Name).ToList<string>();
					List<string> list3 = (from e in this.m_lstFamilyLibWins
					select e.Name).ToList<string>();
					foreach (string text in list)
					{
						this.treeViewDoor.Nodes.Add(text);
					}
					foreach (string text2 in list2)
					{
						this.treeViewRollingDoor.Nodes.Add(text2);
					}
					using (List<string>.Enumerator enumerator = list3.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							string text3 = enumerator.Current;
							this.treeViewWindow.Nodes.Add(text3);
						}
						goto IL_37D;
					}
				}
				IEnumerable<string> enumerable = (from e in new FilteredElementCollector(this.m_doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).Cast<FamilyInstance>().Where(new Func<FamilyInstance, bool>(DlgFireOutlineWinDoorWallFamilyReplace.IsValidFireDoors)).ToList<FamilyInstance>()
				select e.Symbol.Family.Name).Distinct<string>();
				IEnumerable<string> enumerable2 = (from e in new FilteredElementCollector(this.m_doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Doors).Cast<FamilyInstance>().Where(new Func<FamilyInstance, bool>(DlgFireOutlineWinDoorWallFamilyReplace.IsValidFireDoorRollings)).ToList<FamilyInstance>()
				select e.Symbol.Family.Name).Distinct<string>();
				IEnumerable<string> enumerable3 = (from e in new FilteredElementCollector(this.m_doc).OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows).Cast<FamilyInstance>().Where(new Func<FamilyInstance, bool>(DlgFireOutlineWinDoorWallFamilyReplace.IsValidFireWindows)).ToList<FamilyInstance>()
				select e.Symbol.Family.Name).Distinct<string>();
				foreach (string text4 in enumerable)
				{
					this.treeViewDoor.Nodes.Add(text4);
				}
				foreach (string text5 in enumerable2)
				{
					this.treeViewRollingDoor.Nodes.Add(text5);
				}
				foreach (string text6 in enumerable3)
				{
					this.treeViewWindow.Nodes.Add(text6);
				}
				IL_37D:;
			}
			catch
			{
			}
		}

		private static bool IsValidFireDoors(FamilyInstance fi)
		{
			string name = fi.Symbol.Family.Name;
			string name2 = fi.Symbol.Name;
			return !UtilityTools.IsValidDoorRollingFamilyName(name) && !UtilityTools.IsValidDoorRollingFamilyName(name2) && UtilityTools.IsValidDoorWinClassAFamilyName(name, name2);
		}

		private static bool IsValidFireDoorRollings(FamilyInstance fi)
		{
			string name = fi.Symbol.Family.Name;
			string name2 = fi.Symbol.Name;
			return (UtilityTools.IsValidDoorRollingFamilyName(name) || UtilityTools.IsValidDoorRollingFamilyName(name2)) && UtilityTools.IsValidFireDoorWinFamilyName(name);
		}

		private static bool IsValidFireWindows(FamilyInstance fi)
		{
			string name = fi.Symbol.Family.Name;
			string name2 = fi.Symbol.Name;
			return UtilityTools.IsValidDoorWinClassAFamilyName(name, name2);
		}

		public string FireDoorClassAFiName
		{
			get
			{
				foreach (object obj in this.treeViewDoor.Nodes)
				{
					TreeNode treeNode = (TreeNode)obj;
					if (treeNode.Checked)
					{
						return treeNode.Text;
					}
				}
				return "";
			}
		}

		public string FireDoorSuperFiName
		{
			get
			{
				foreach (object obj in this.treeViewRollingDoor.Nodes)
				{
					TreeNode treeNode = (TreeNode)obj;
					if (treeNode.Checked)
					{
						return treeNode.Text;
					}
				}
				return "";
			}
		}

		public string FireWindowClassAFiName
		{
			get
			{
				foreach (object obj in this.treeViewWindow.Nodes)
				{
					TreeNode treeNode = (TreeNode)obj;
					if (treeNode.Checked)
					{
						return treeNode.Text;
					}
				}
				return "";
			}
		}

		private Document m_doc;

		private ExternalCommandData m_cmdData;

		public List<DbFamily> m_lstFamilyLibDoors;

		public List<DbFamily> m_lstFamilyLibWins;

		public List<DbFamily> m_lstFamilyLibDoorsRolling;
	}
}
